use std::io;
use std::cmp::Ordering;
use rand::Rng;

fn main() {
    println!("猜数字游戏！");
    let secret_num = rand::thread_rng().gen_range(1..=100);
    // println!("生成的数字是: {}", secret_num);
    let mut min = 1;
    let mut max = 100;

    let mut count = 5;
    while count > 0 {
        println!("请输入一个数字：范围 [{}, {}], 剩余次数: {}", min, max, count);
        count -= 1;

        let mut guess = String::new();

        io::stdin().read_line(&mut guess).expect("无法读取行");

        let guess: u8 = match guess.trim().parse() {
            Ok(num) => num,
            Err(_) => continue
        };
        println!("你输入的数是: {}", guess);

        match guess.cmp(&secret_num) {
            Ordering::Less => {
                println!("太小了");
                if min <= guess {
                    min = guess + 1;
                }
            }
            Ordering::Equal => {
                println!("恭喜猜对了");
                return;
            }
            Ordering::Greater => {
                println!("太大了");
                if max >= guess {
                    max = guess - 1;
                }
            }
        }
    }
    println!("失败了，数字是{}", secret_num);
}
